001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Dec 1, 2002
005     * Time: 1:16:41 AM
006     */
007    
008    package EVolve.visualization.XYViz.ValValViz;
009    
010    import EVolve.visualization.*;
011    import EVolve.visualization.Dimension;
012    import EVolve.visualization.VizFactory.VisualizationFactory;
013    import EVolve.Scene;
014    import EVolve.exceptions.VizInfoCreateException;
015    import EVolve.util.painters.MissRatePainter;
016    import EVolve.util.painters.shapes.Line;
017    import EVolve.util.predefinedutils.VizInfo;
018    import EVolve.data.*;
019    import javax.swing.*;
020    import java.awt.*;
021    import java.awt.event.*;
022    import java.util.HashMap;
023    
024    public class MissPredictionMetricViz extends ValueValueVisualization {
025        private PredictorFactory[] factory; // predictor factories
026        private Predictor[] predictor; // predictors
027        private JComboBox comboPredictor; // combobox for selecting predictor factory
028        private PredictorFactory selectedFactory; // predictor factory in use
029        private JMenuItem itemSelectTimeFrame;
030        protected static JMenuItem selectionMenu[] = null;
031        private static int SELECT_OPTION;
032        protected JTextField textInterval;
033    
034        public MissPredictionMetricViz(PredictorFactory[] factory) {
035            super();
036            interval = 1000;
037            this.factory = factory;
038            SELECT_OPTION = 0x0001;
039        }
040    
041        public Dimension[] createDimension() {
042            Dimension[] returnVal = new Dimension[3];
043            xAxis = new ValueDimension();
044            entityIdFilter = new ReferenceDimension();
045            filter2 = new ReferenceDimension();
046    
047            returnVal[0] = xAxis;
048            returnVal[1] = entityIdFilter;
049            returnVal[2] = filter2;
050    
051            return returnVal;
052        }
053    
054        protected void updateConfiguration() {
055            try {
056                if (autoInterval != -1) {
057                    interval = autoInterval;
058                    autoInterval = -1;
059                } else
060                    interval = Integer.parseInt(textInterval.getText());
061    
062                super.updateConfiguration();
063                canvas.setName("Bytecode", "Miss rate");
064            } catch (NumberFormatException e) {
065                Scene.showErrorMessage("Interval must be an integer");
066                configure();
067            }
068        }
069    
070        protected JPanel createConfigurationPanel() {
071            JPanel configurationPanel = new JPanel(new GridLayout(2, 1, 5, 5));
072    
073            selectedFactory = factory[0];
074    
075            JPanel panelTop = new JPanel(new FlowLayout());
076            configurationPanel.add(panelTop);
077    
078            panelTop.add(new JLabel("Predictor: "));
079    
080            comboPredictor = new JComboBox();
081            for (int i = 0; i < factory.length; i++) {
082                comboPredictor.addItem(factory[i].getName());
083            }
084            panelTop.add(comboPredictor);
085    
086    
087            JPanel panelBottom = new JPanel(new FlowLayout());
088            panelBottom.add(new JLabel("Interval: "));
089    
090            textInterval = new JTextField(String.valueOf(interval), 10);
091            panelBottom.add(textInterval);
092    
093            configurationPanel.add(panelBottom);
094    
095            return configurationPanel;
096        }
097    
098        public void preVisualize() {
099            predictor = new Predictor[entityIdFilter.getMaxEntityNumber()];
100            for (int i = 0; i < predictor.length; i++) {
101                predictor[i] = selectedFactory.createPredictor();
102            }
103    
104            image = new AutoShapeImage(new Line(0,0,1));
105            xMax = 0;
106            installPainter();
107            super.preVisualize();
108        }
109    
110        public void receiveElement(Element element) {
111            if (element.isOptional()) return;
112    
113            long temp = xAxis.getField(element);
114            long x = temp / interval;
115            long y = entityIdFilter.getField(element);
116            long z = filter2.getField(element);
117    
118            countEvents(temp);
119    
120            if (xOffset == -1) xOffset = x;
121            painter.paint(image,x-xOffset,y,filter2.getEntityFromInt((int)z).getId());
122    
123            if (temp > xMax) {
124                xMax = temp;
125            }
126        }
127    
128        public void visualize() {
129            canvas.setName(xAxis.getName() + " (" + xMax + ")","Miss Prediction rate (" +
130                          ((MissRatePainter)painter).getMaxMiss()*100 + "%)");
131            sort();
132        }
133    
134        protected void mouseMove(int x, int y) {
135            int X = canvas.getImageX(x);
136            if ((X >= 0) && (X < image.getW())) {
137                Scene.setStatus(((MissRatePainter)painter).getMissrate(X)+"%");
138            } else {
139                Scene.setStatus(" ");
140            }
141        }
142    
143        protected void installPainter() {
144            painter = new MissRatePainter(predictor,filter2.getDataFilter().getTargetType());
145        }
146    
147        public void makeSelection() {
148            int x1 = canvas.getStartX();
149            int x2 = canvas.getEndX();
150    
151            if (dataSourceId != Scene.getDataSourceManager().getCurrentDataSourceId()) {
152                Scene.showErrorMessage("The active data source used currently is different from \n" +
153                                       "this visualization, please choose \"" +
154                                       Scene.getDataSourceManager().getUsedDataSourceName(dataSourceId)+"\".");
155                return;
156            }
157    
158            if (((x1<0)&&(x2<0)) || ((x1>=timeMap.size()))&&(x2>=timeMap.size()))
159               return;
160    
161            if (x1 < 0) {
162                x1 = 0;
163            }
164    
165            if (x2 > (timeMap.size() - 1)) {
166                x2 = timeMap.size() - 1;
167            }
168    
169            int[] selection = new int[entityIdFilter.getEntityNumber()];
170            for (int i = 0; i < selection.length; i++) {
171                selection[i] = i;
172            }
173    
174    
175            entityIdFilter.makeSelection(subjectDefinition.getType(),selection,((long[])timeMap.get(x1))[1],
176                                         ((long[])timeMap.get(x2))[1],timeMap);
177    
178        }
179    
180        public HashMap getCurrentConfigure() {
181            try {
182                HashMap configure = super.getCurrentConfigure();
183    
184                VizInfo vizInfo = new VizInfo();
185                vizInfo.setFactory((VisualizationFactory)configure.get("Factory"));
186                vizInfo.setSubject((ElementDefinition)configure.get("Subject"));
187    
188                String[] dimensionDefs = new String[3];
189                dimensionDefs[0] = xAxis.getName() ;
190                dimensionDefs[1] = entityIdFilter.getName() ;
191                dimensionDefs[2] = filter2.getName() ;
192                configure.put("Dimension",vizInfo.createDimension(dimensionDefs));
193                configure.put("Predictor",selectedFactory);
194    
195                return configure;
196            } catch (VizInfoCreateException e) {
197                Scene.showErrorMessage(e.getMessage());
198            }
199            return null;
200        }
201    
202        public JMenuItem[] createSelectionMenuItem() {
203            itemSelectTimeFrame = new JCheckBoxMenuItem("Time Frame");
204            itemSelectTimeFrame.setMnemonic(KeyEvent.VK_T);
205            itemSelectTimeFrame.setSelected(true);
206            itemSelectTimeFrame.setEnabled(false);
207    
208    
209            JMenuItem[] items = new JMenuItem[1];
210            items[0] = itemSelectTimeFrame;
211    
212            return items;
213        }
214    
215        public Object clone() {
216            MissPredictionMetricViz o = (MissPredictionMetricViz) super.clone();
217            o.dimension[0] = o.xAxis;
218            o.dimension[1] = o.entityIdFilter;
219            o.dimension[2] = o.filter2;
220            o.factory = new PredictorFactory[factory.length];
221            for (int i=0; i<factory.length; i++) {
222                o.factory[i] = (PredictorFactory)factory[i].clone();
223            }
224            o.selectedFactory = (PredictorFactory)selectedFactory.clone();
225            if (predictor != null) {
226                o.predictor = new Predictor[predictor.length];
227                for (int i=0; i<predictor.length; i++)
228                    o.predictor[i] = (Predictor)predictor[i].clone();
229            }
230            o.createDialog();
231            return o;
232        }
233    
234    }